<?php
namespace App\Models;

class PayLog extends BaseModel
{
    protected $table = "pay_log";

    public $timestamps = false;

    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 按条件查询单条数据
     */
    public function getOne(array $where, $fields = '*')
    {
        return $this->multiSelect($fields)->multiWhere($where)->first();
    }

    /**
     * 按条件查询全部数据,根据配置显示条数显示
     */
    public function getInfoList(array $where = [], $fields = '*', $order = '', $pageSize = '')
    {
        if ($pageSize) {
            return $this->multiSelect($fields)->with('charge')->with('order')->with('user')->multiWhere($where)->multiOrder($order)->paginate($pageSize);
        } else {
            return $this->multiSelect($fields)->with('charge')->with('order')->with('user')->multiWhere($where)->multiOrder($order)->get();
        }
    }

    public function getList(array $where = [], $fields = '*', $order = '', $pageSize = '')
    {
        if ($pageSize) {
            return $this->multiSelect($fields)->multiWhere($where)->multiOrder($order)->paginate($pageSize);
        } else {
            return $this->multiSelect($fields)->multiWhere($where)->multiOrder($order)->get();
        }

    }

    /**
     * 根据条件删除数据
     */
    public function del(array $where)
    {
        return $this->multiWhere($where)->delete();
    }

    /**
     *插入数据
     */
    public function add($data)
    {
//        dd($data);
        return $this->insertGetId($data);
    }

    /**
     *更新数据
     */
    public function up($where, $data)
    {
        return $this->multiWhere($where)->update($data);
    }

    /**
     * 获取和
     */
    public function getSum($where, $field)
    {
        return $this->multiWhere($where)->sum($field);
    }

    /**
     * 字段递加
     */
    public function inc($where, $field, $n)
    {
        return $this->multiWhere($where)->increment($field, $n);
    }

    public function charge()
    {
        return $this->belongsTo('App\Models\Charge', 'charge_id', 'id');
    }

    public function user()
    {
        return $this->belongsTo('App\Models\User', 'user_id', 'id');
    }

    public function order()
    {
        return $this->belongsTo('App\Models\Appointment', 'appointment_id', 'id');
    }

    /**
     *  获取交易明细
     */
    public function getLogList($request)
    {
        $where = [];
        $start = strtotime($request['start']);
        $end = strtotime($request['end']);

        if ( $start && $end ) {
            $start1 = $start;
            $end1 = $end;
            if($start>$end){
                $start1 = $end;
                $end1 = $start;
            }
            $where['add_time'] = ['glt',[['>=',$start1],['<',$end1]]];
        }
        $order['id'] = 'desc';
        $data = $this->getInfoList($where, "*", $order, 10);
        if ($data) {
            $data = $data->toArray();
            $data['pageNoList'] = getPageNoList($data['last_page'], request('page', 1), 10);
        }
        return $data;
    }

    /**
     *  获取交易明细
     */
    public function getMyLogList($request)
    {
        $where = [];
        $uid = $request['mid'];
        $where['user_id'] = $uid;
        $order['id'] = 'desc';
        $data = $this->getList($where, "*", $order, 10);
        if ($data) {
            $data = $data->toArray();
            $data['pageNoList'] = getPageNoList($data['last_page'], request('page', 1), 10);
        }
        $user = new User();
        $appoint = new Appointment();
        $charge = new Charge();
        foreach ($data['data'] as $k => $v) {
            $u_info = $user->getUserById(array('id' => $v['user_id']));
            $data['data'][$k]['user_name'] = $u_info['data']['wx_name'];
            if ($v['type'] == 0) {
                $appoint_where['id'] = $v['appointment_id'];
                $appoint_info = $appoint->getOne($appoint_where);
                if ($appoint_info) {
                    $appoint_info = $appoint_info->toArray();
                }
                $data['data'][$k]['money'] = $appoint_info['register_price'];
            }
            if ($v['type'] == 1) {
                $charge_where['id'] = $v['charge_id'];
                $charge_info = $charge->getOne($charge_where);
                if ($charge_info) {
                    $charge_info = $charge_info->toArray();
                }
                $data['data'][$k]['money'] = $charge_info['money'];
            }
        }
        return $data;
    }

    /**
     *
     * @return mixed 充值记录
     */
    public function charge_log()
    {
        $where = [];
        $order['id'] = 'desc';
        $where['type'] = 1;
        $rows = $this->getList($where, '*', $order, 20);
        if ($rows) {
            $rows = $rows->toArray();
            $rows['pageNoList'] = getPageNoList($rows['last_page'], request('page', 1), 20);
        }
        return $rows;
    }

    public function addPayLog($uid, $type, $aId = 0, $chargeId = 0)
    {
        $data['user_id'] = $uid;
        $data['type'] = $type;
        $data['appointment_id'] = $aId;
        $data['charge_id'] = $chargeId;
        $data['add_time'] = time();
        return $this->add($data);
    }

    /**
     *  代付账单
     */
    public function his_get_bill_list($user_id, $m_id, $cardno)
    {
        $data['vaa01'] = $m_id;
        $data['sca01'] = $user_id;
        $data['cardno'] = $cardno;
        return $data;
    }

    /**
     * 获取会员消费记录
     */
    public function his_membertrade_record($cardno)
    {
        $data['cardno'] = $cardno;
        return $data;
    }
}